package com.rboat.web.controller.blog;

import com.rboat.blog.domain.BlogWord;
import com.rboat.blog.service.IBlogWordService;
import com.rboat.common.annotation.Log;
import com.rboat.common.core.controller.BaseController;
import com.rboat.common.core.domain.AjaxResult;
import com.rboat.common.core.page.TableDataInfo;
import com.rboat.common.enums.BusinessType;
import com.rboat.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 留言Controller
 * 
 * @author rboat
 * @date 2019-11-05
 */
@Controller
@RequestMapping("/blog/word")
public class BlogWordController extends BaseController
{
    private String prefix = "blog/word";

    @Autowired
    private IBlogWordService blogWordService;

    @RequiresPermissions("blog:word:view")
    @GetMapping()
    public String word()
    {
        return prefix + "/word";
    }

    /**
     * 查询留言列表
     */
    @RequiresPermissions("blog:word:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BlogWord blogWord)
    {
        startPage();
        List<BlogWord> list = blogWordService.selectBlogWordList(blogWord);
        return getDataTable(list);
    }

    /**
     * 导出留言列表
     */
    @RequiresPermissions("blog:word:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BlogWord blogWord)
    {
        List<BlogWord> list = blogWordService.selectBlogWordList(blogWord);
        ExcelUtil<BlogWord> util = new ExcelUtil<BlogWord>(BlogWord.class);
        return util.exportExcel(list, "word");
    }

    /**
     * 新增留言
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存留言
     */
    @RequiresPermissions("blog:word:add")
    @Log(title = "留言", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BlogWord blogWord)
    {
        return toAjax(blogWordService.insertBlogWord(blogWord));
    }

    /**
     * 修改留言
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        BlogWord blogWord = blogWordService.selectBlogWordById(id);
        mmap.put("blogWord", blogWord);
        return prefix + "/edit";
    }

    /**
     * 修改保存留言
     */
    @RequiresPermissions("blog:word:edit")
    @Log(title = "留言", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BlogWord blogWord)
    {
        return toAjax(blogWordService.updateBlogWord(blogWord));
    }

    /**
     * 删除留言
     */
    @RequiresPermissions("blog:word:remove")
    @Log(title = "留言", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(blogWordService.deleteBlogWordByIds(ids));
    }
}
